package com.mryin.performance.controller;

import com.alibaba.excel.metadata.BaseRowModel;
import com.mryin.performance.pojo.Task;
import com.mryin.performance.service.TaskService;
import com.mryin.performance.utils.*;
import com.sun.rowset.internal.BaseRow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: PerformanceManager
 * @description: 数据导入的控制器
 * @author: Mr.YIN
 * @create: 2021-03-10 14:00
 **/
@RestController
@RequestMapping("/Excel")
public class ExcelController {

    @Autowired
    private TaskService taskService;

    @RequestMapping(value = "/ajaxUpload")
    public Map<String, Object> ajaxUploadExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("进来了");
        System.out.println(file);
        Map<String, Object> map = new HashMap<>();
        String s = taskService.inputExcel(file, request, response);
        System.out.println("我是导入的seviceimpl中的返回结果" + s);
        map.put("code", 0);
        map.put("msg", "导入成功");
        return map;
    }

    /*@RequestMapping(value = "/addTask", method = RequestMethod.POST)
    public DataGridView addstudents(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        //1.获取文件名
        String sname = file.getOriginalFilename();
        //2.上传之后的名字
        String newsname = System.currentTimeMillis() + sname;
        //3.拿到一个位置，把文件放到的位置(默认是webapp下的)
        String path = request.getServletContext().getRealPath("/updlod");
        //4.拼接路径
        File temfile = new File(path + File.separator + newsname);
        //5.上传
        /*try {
            //file.transferTo(temfile);
            //6.读取文件

            FileInputStream in = new FileInputStream(file.getOriginalFilename());
            //7.通过poi解析流HSSFWorkbook处理流得到的对象，就是封装的excel文件所有的数据
            HSSFWorkbook worstu = new HSSFWorkbook(in);
            //8.获取excel文件里面的工作表sheet,下表从0开始，
            HSSFSheet sheet = worstu.getSheetAt(0);
            //9.获取工作表数据，从第二行数据开始读取，读到最后一行结束getLastRowNum()
            int sum = sheet.getLastRowNum();
            System.out.println(sum);
            //10.用for循环取到每行，然后在取到每一行的每一个单元格里的数据，在封装到实体里，然后添加到数据库中
            //for循环i开始用1而不用0的原因：因为sheet工作表的第一行不是数据而是表明数据的字段
            for (int i = 1; i <= sum; i++) {
                //取到每一行数据
                HSSFRow row = sheet.getRow(i);

                String id = row.getCell(0).getStringCellValue();
                int  monovalent = (int) row.getCell(1).getNumericCellValue();
                int marketprice = (int) row.getCell(2).getNumericCellValue();
                String name = row.getCell(3).getStringCellValue();
                Task task = new Task();
                task.setId(id).setMonovalent(monovalent).setMarketprice(marketprice).setName(name);
                taskService.save(task);
                //取到每一行第一个单元格的数据
                String sson = String.valueOf((int) row.getCell(0).getNumericCellValue());
                //取到每一行第二个单元格的数据
                String ssname = row.getCell(1).getStringCellValue();
                //取到每一行第三个单元格的数据
                String ssex = row.getCell(2).getStringCellValue();
                //取到每一行第四个单元格的数据
                String smajar = row.getCell(3).getStringCellValue();
                //取到每一行第五个单元格的数据
                String sclass = row.getCell(4).getStringCellValue();
                //取到每一行第六个单元格的数据
                String spassword = row.getCell(5).getStringCellValue();
                //取到每一行第七个单元格的数据
                String sisenter = row.getCell(6).getStringCellValue();
                //取到每一行第八个单元格的数据
                String slock = row.getCell(7).getStringCellValue();
                //取到每一行第九个单元格的数据
                int smax = (int) row.getCell(8).getNumericCellValue();
                //取到每一行第十个个单元格的数据
                int srole = (int) row.getCell(9).getNumericCellValue();
                //拿到所有数据在new一个实体，放在实体里去添加到数据库
                Student st = new Student(ssname, ssex, sson, smajar, spassword, sclass, sisenter, slock, smax, srole);


                //这里用的SpringBootJPA自带的添加方法，添加到数据库
                Student list = studentService.addshudent(st);
            }
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new DataGridView(0,"导入成功");
    }*/



    @RequestMapping("/importExcel")
    public DataGridView import_order(MultipartFile file) throws ExcelException {

        Object objList = ExcelUtil.readExcel(file,new Task(),1,1);
        if(null == objList){
            return new DataGridView(-1,"读取失败");
        }

        List taskList = (List) objList;

        if(null == taskList || taskList.size() == 0){
            return new DataGridView(-1,"读取失败");
        }

        for (Object o : taskList) {
            Task task = (Task) o;
            taskService.save(task);
            //taskService.addTaskByReadExcel(task)
        }

        taskList.forEach(System.out::println);
        return new DataGridView(0,"读取成功");
    }






}
